package hotel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class loginWindow implements ActionListener
{
	protected static JFrame testframe; // static so the class progressBar can dispose of it. 
	private JPanel panel;
	private JFrame loginFrame;
	private JTextField idField, codeField;
	private JPasswordField passwordField;
	private JButton loginButton, cancelButton;
	private String type = "User";
	protected static Database database;

	public loginWindow(Database d){
		database = d;
		panel = new JPanel();
		try {
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		loginFrame = new JFrame("Login");
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		loginFrame.setSize(new Dimension(700, 150));
		loginFrame.setResizable(false);
		loginFrame.setLocation((int) (screenSize.width*0.25), (int) (screenSize.height*0.25));
		loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		loginFrame.setLayout(new BorderLayout());
		createIdAndPasswordField();
		createButtons();
		loginFrame.add(panel, BorderLayout.CENTER);
		loginFrame.setVisible(true);
	}

	/**
	 * Void method that creates the ID and Password Fields and adds them to the panel. 
	 */
	private void createIdAndPasswordField()
	{
		JPanel passwordPanel = new JPanel();
		passwordPanel.setBackground(Color.white);
		JPanel idPanel = new JPanel();
		JPanel codePanel = new JPanel();
		idField = new JTextField(20);
		idField.setEditable(true);
		idField.setCaretPosition(0);
		idField.setBorder(new LineBorder(Color.BLACK, 1, true));

		codeField = new JTextField(30);
		codeField.setEditable(true);
		codeField.setCaretPosition(0);
		codeField.setBorder(new LineBorder(Color.BLACK, 1, true));

		String[] comboBoxNames = { "User", "Hotel Owner"};
		JComboBox combo = new JComboBox(comboBoxNames);
		combo.setSelectedIndex(0);
		combo.addActionListener(this);
		combo.setActionCommand("combo");

		JTextArea idTxtArea = new JTextArea("Username :");
		idTxtArea.setEditable(false);
		idTxtArea.setHighlighter(null);
		idPanel.add(idTxtArea);
		idPanel.add(idField);

		idPanel.setBackground(Color.white);

		passwordField = new JPasswordField(20);
		passwordField.setEditable(true);
		passwordField.setCaretPosition(0);
		passwordField.setBorder(new LineBorder(Color.BLACK, 1, true));

		JTextArea passwordTxtArea = new JTextArea("Password :");
		passwordTxtArea.setEditable(false);
		passwordTxtArea.setHighlighter(null);
		JTextArea codeTxtArea = new JTextArea("License Code : ");
		codeTxtArea.setEditable(false);
		codeTxtArea.setHighlighter(null);

		codePanel.add(codeTxtArea);
		codePanel.add(codeField);

		passwordPanel.add(passwordTxtArea);
		passwordPanel.add(passwordField);
		loginFrame.add(combo, BorderLayout.NORTH);
		panel.add(idPanel);
		panel.add(passwordPanel);
		panel.setBackground(Color.white);
	}

	/**
	 * Void methods that creates the buttons and adds them to the panel.
	 */
	public void createButtons()
	{
		JPanel buttonPanel = new JPanel(new FlowLayout());
		loginButton = new JButton("Login");
		loginButton.setActionCommand("Login");
		loginButton.addActionListener(this);
		cancelButton = new JButton("Clear");
		cancelButton.setActionCommand("Clear");
		cancelButton.addActionListener(this);
		JButton CreateButton = new JButton();
		CreateButton = new JButton("Create Account");
		CreateButton.setActionCommand("Create");
		CreateButton.addActionListener(this);
		buttonPanel.add(loginButton);
		buttonPanel.add(new JSeparator());
		buttonPanel.add(cancelButton);
		buttonPanel.add(CreateButton);
		panel.add(new JSeparator());
		panel.add(new JSeparator());
		panel.add(new JSeparator());
		panel.add(buttonPanel);
		Container cp = loginFrame.getContentPane();
		cp.setBackground(Color.white);
	}

	private boolean checkUserAccount(String name, String pass) throws FileNotFoundException{
		boolean toReturn = false;
		ArrayList<User> users = database.getUserList();
		for(int i = 0; i < users.size(); i++){
			if(  users.get(i).getName().equals(name) && users.get(i).getPassword().equals(pass) ){
				toReturn = true;	
			}
		}
		return toReturn;
	}
	private boolean checkOwnerAccount(String name, String pass) {
		boolean toReturn = false;
		ArrayList<Owner> owners = database.getOwnerList();
		for(int i = 0; i < owners.size(); i++){
			if(  owners.get(i).getName().equals(name) && owners.get(i).getPassword().equals(pass) ){
				toReturn = true;	
			}
		}
		return toReturn;
	}
	/**
	 * Action Performed method that overrides actionListener's actionPerformed method.  
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String c = e.getActionCommand();
		if(c.equals("Clear")) //if the clear button is pressed, sets the fields to be blank
		{
			idField.setText("");
			passwordField.setText("");
		} 
		else if(c.equals("combo")) //attempt to login
		{
			JComboBox cb = (JComboBox)e.getSource();
			type = (String)cb.getSelectedItem();
		}
		else if(c.equals("Login")) //attempt to login
		{
			try {
				String user = idField.getText();
				if (checkUserAccount(idField.getText(), passwordField.getText()) && type == "User")
				{
				
					ArrayList<User>	users = database.getUserList();
					User userIn = users.get(0);
					for(int i = 0; i< users.size(); i++){
						if(user.equals(users.get(i).getName())){
							userIn = users.get(i);
						}
					}
					new userApplication(userIn);
					loginFrame.dispose();
				}
				else if (checkOwnerAccount(idField.getText(), passwordField.getText()) && type == "Hotel Owner")
				{
					new ownerApplication(idField.getText());
					loginFrame.dispose();
				}
				else{
					JOptionPane.showMessageDialog(null, "User/Password combination not found", "Error", JOptionPane.PLAIN_MESSAGE);
				}

			} catch (FileNotFoundException e1) {

			}
		}
		else if(c.equals("Create")) //attempt to login
		{
			new createAccount(type);
		}
	}


}


